//
//  ForgetPassWordViewController.m
//  XMDLearn
//
//  Created by apple on 16/4/12.
//  Copyright © 2016年 hao. All rights reserved.
//

#import "ForgetPassWordViewController.h"
#import "LoginViewController.h"
#import <SMS_SDK/SMSSDK.h>
#import <CRToast.h>
#import "MBProgressHUD.h"
#import "TrimWhitespace.h"

@interface ForgetPassWordViewController ()

@property (weak, nonatomic) IBOutlet UIView *HeadView;

@property (weak, nonatomic) IBOutlet UITextField *VerityTextField;

@property (weak, nonatomic) IBOutlet UITextField *PassWordTextField;

@property (weak, nonatomic) IBOutlet UIButton *VerityButton;

@property (strong,nonatomic) NSTimer *timer;
@property (assign,nonatomic) NSInteger timeCount;

@property (strong,nonatomic) MBProgressHUD *HUD;

@end

@implementation ForgetPassWordViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
   // [self.view resignFirstResponder];
    
    self.timeCount = 20;
    
    self.HeadView.backgroundColor = [UIColor colorWithRed:225/255.0 green:39/255.0 blue:42/255.0 alpha:1.0];
    self.VerityButton.backgroundColor = [UIColor colorWithRed:248.0/255.0 green:70.0/255.0 blue:55.0/255.0 alpha:0.85];
    
    // 添加手势
    UITapGestureRecognizer *tap1 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(viewTapped:)];
    tap1.cancelsTouchesInView = NO;
    [self.view addGestureRecognizer:tap1];
}

// 点击空白收回键盘
-(void)viewTapped:(UITapGestureRecognizer*)tap1
{
    
    [self.view endEditing:YES];
    
}

//-(IBAction)toogleback:(id)sender{
//    
//    [self.view endEditing:YES];
//}

- (IBAction)VerityBtn:(id)sender {
    
    NSUserDefaults *userdefaults = [NSUserDefaults standardUserDefaults];
    
    self.timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timeLee) userInfo:nil repeats:YES];
        self.timeCount = 20;
    [SMSSDK getVerificationCodeByMethod:SMSGetCodeMethodSMS phoneNumber:[userdefaults objectForKey:@"name"] zone:@"86" customIdentifier:nil result:^(NSError *error) {
            if (!error) {
                [self showInformationWithText:@"获取验证码成功!"];
                
            }else{
                [self showInformationWithText:@"获取验证码失败！"];
            }
    }];
}

- (IBAction)BackBtn:(id)sender {
    
    LoginViewController *login = [[LoginViewController alloc]init];
    
    [self presentViewController:login animated:YES completion:nil];
}

-(void)timeLee{
    
    self.timeCount--;
    if (self.timeCount > 0) {
        [self.VerityButton setTitle:[NSString stringWithFormat:@"%ld",(long)self.timeCount] forState:UIControlStateNormal];
        self.VerityButton.userInteractionEnabled = NO;
        self.VerityButton.backgroundColor = [UIColor grayColor];
    } else {
        [self.VerityButton setTitle:[NSString stringWithFormat:@"获取验证码"] forState:UIControlStateNormal];
        if (self.timeCount == 0) {
            [self.timer invalidate];
        }
        self.VerityButton.userInteractionEnabled = YES;
        self.VerityButton.backgroundColor = [UIColor colorWithRed:248.0/255.0 green:70.0/255.0 blue:55.0/255.0 alpha:0.85];
    }
    
}

- (void)showInformationWithText:(NSString *)text{
    NSDictionary *options = @{
                              kCRToastTextKey : text,
                              kCRToastTextAlignmentKey : @(NSTextAlignmentCenter),
                              kCRToastBackgroundColorKey : [UIColor redColor],
                              kCRToastAnimationInTypeKey : @(CRToastAnimationTypeLinear),
                              kCRToastAnimationOutTypeKey : @(CRToastAnimationTypeGravity),
                              kCRToastAnimationInDirectionKey : @(CRToastAnimationDirectionTop),
                              kCRToastAnimationOutDirectionKey : @(CRToastAnimationDirectionTop),
                              kCRToastNotificationTypeKey :
                                  @(CRToastTypeNavigationBar),
                              kCRToastTextColorKey :
                                  [UIColor lightGrayColor],
                              kCRToastFontKey :
                                  [UIFont boldSystemFontOfSize:18]
                              };
    [CRToastManager showNotificationWithOptions:options
                                completionBlock:^{
                                    NSLog(@"Completed");
                                }];
}


- (IBAction)ForGetPassWordBtn:(id)sender {
    
    if ([self validataLoginData]) {
        
        // 跳转到登录页面
    }
}


-(BOOL)validataLoginData{
    
    if ([TrimWhitespace isNullOrEmptyOrWhiteSpace:self.PassWordTextField.text]) {
        [self showHUD:@"请输入密码"];
        return NO;
    }
    if ([TrimWhitespace isNullOrEmptyOrWhiteSpace:self.VerityTextField.text]) {
        [self showHUD:@"请输入验证码"];
        return NO;
    }
    if (self.PassWordTextField.text.length < 6 || self.PassWordTextField.text.length > 20) {
        [self showHUD:@"请输入最低6位最高20位密码"];
        return NO;
    }
    return YES;
    
}

-(void)showHUD:(NSString*)text{
    
    self.HUD = [[MBProgressHUD alloc]initWithView:self.view];
    
    [self.view addSubview:self.HUD];
    self.HUD.labelText = text;
    self.HUD.mode = MBProgressHUDModeText;
    
    self.HUD.yOffset = 100.0f;
    self.HUD.xOffset = 0.0f;
    
    [self.HUD showAnimated:YES whileExecutingBlock:^{
        sleep(1.5);
    } completionBlock:^{
        [self.HUD removeFromSuperview];
        self.HUD = nil;
    }];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
